function showNotes(){
    //给选中的笔记本加选中的状态
    $("#book_list li a").removeClass("checked")
    $(this).find("a").addClass("checked")
    //this指的是li
    var bookId = $(this).data("bookId");
    $.ajax({
        url: base_url + "/note/loadnotes.do",
        type: "get",
        data: {
            "bookId": bookId,
        },
        dataType: "json",
        success : function(data) {
            if (data.status==0) {
                var notes=data.data;
                //清空上一次点击显示的笔记
                $("#note_list").empty();
                for (var i = 0; i < notes.length; i++) {
                    var noteId=notes[i].noteId;
                    var noteName=notes[i].noteTittle;
                    var s_li ='<li class="online" >'
                    s_li += '<a>'
                    s_li += '<i class="fa fa-file-text-o" title="online" rel="tooltip-bottom">'
                    s_li += '</i> '+noteName
                    s_li += '<button type="button" class="btn btn-default btn-xs btn_position btn_slide_down"><i class="fa fa-chevron-down"></i></button>'
                    s_li += '</a>'
                    s_li +='<div class="note_menu" tabindex=\'-1\'>'
                    s_li += ' <dl>'
                    s_li += '<dt><button type="button" class="btn btn-default btn-xs btn_move" title=\'移动至...\'><i class="fa fa-random"></i></button></dt> '
                    s_li += '<dt><button type="button" class="btn btn-default btn-xs btn_share" title=\'分享\'><i class="fa fa-sitemap"></i></button></dt> '
                    s_li += '<dt><button type="button" class="btn btn-default btn-xs btn_delete" title=\'删除\'><i class="fa fa-times"></i></button></dt> '
                    s_li += ' </dl>'
                    s_li += ' </div>'
                    s_li += '</li>'
                    var $li = $(s_li)//将html字符串转换成jquery对象
                    $li.data("noteId",noteId)
                    //将li元素添加到ul
                    $("#note_list").append($li)
                }
                return false;
            }
        },
        error:function(data) {

        }
    })
}
function showDetails(){
    console.log(1)
    $("#note_list li a").removeClass("checked")
    $(this).find("a").addClass("checked")
    var noteId = $(this).data("noteId");
    $.ajax({
        url: base_url + "/note/loadnote.do",
        type: "get",
        data: {
            "noteId": noteId,
        },
        dataType: "json",
        success:function(data) {
            if (data.status==0) {
                var note=data.data;
                var noteTittle=note.noteTittle
                var noteBody=note.noteBody
                $("#input_note_title").val(noteTittle)
                //得到um编辑器,并设置内容
                var um = UM.getEditor('myEditor');
                um.setContent(noteBody)
                return false;
            }
        },
        error : function(data) {

        }
    })
}
function addBook(){
    //半透明背景显示
    $(".opacity_bg").show();
    //跳转添加页面
    var url=base_url + "/alert/alert_notebook.html"
    //can div中加载此页面
    $("#can").load(url);
    return false;
}
function addNote(){
    var $bookLi=$("#book_list a.checked")
    if ($bookLi.length==0) {
        alert("请选择一个笔记本")
    }
    else {
        //半透明背景显示
        $(".opacity_bg").show();
        //跳转添加页面
        var url=base_url + "/alert/alert_note.html"
        //can div中加载此页面
        $("#can").load(url);
        return false;
    }
}
function editNote(){
    var $li=$("#note_list li a.checked").parent();
    if($li.length==0){
        alert("请先选择一个笔记")
        return;
    }
    var noteId=$li.data("noteId")
    var um = UM.getEditor('myEditor');
    var noteBody = um.getContentTxt();
    var noteTittle=$("#input_note_title").val().trim();
    $.ajax({
        url: base_url +"/note/update.do",
        type: "post",
        data: {
            "noteId":noteId,
            "noteTittle":noteTittle,
            "noteBody":noteBody
        },
        dataType: "json",
        success:function(data) {
            if (data.status==0) {
                //note list中更新标题,需要拼接
                var str='<i class="fa fa-file-text-o" title="online" rel="tooltip-bottom"></i>'+noteTittle+'<button type="button" class="btn btn-default btn-xs btn_position btn_slide_down"><i class="fa fa-chevron-down"></i></button>'
                $li.find("a").html(str);
                alert("恭喜,修改成功")
                return; false;
            }else {
                alert("修改失败")
                return false;
            }
        },
        error : function(data) {
            alert("修改失败,稍后重试")
            return false;
        }
    })
}
function showMenu(){
    //先清空之前点击的
    $(".note_menu").hide()
    var $li=$(this).parents("li")
    let $menu = $li.find(".note_menu");
    $menu.slideDown(1000)
}
function sureDelete(){
    //半透明背景显示
    $(".opacity_bg").show();
    //跳转添加页面
    var url=base_url + "/alert/alert_delete_note.html"

    //can div中加载此页面
    $("#can").load(url);
    return false;
}
function shareNote(){
    let $li = $("#note_list a.checked").parent("li");
    let noteId = $li.data("noteId");
    $.ajax({
        url: base_url +"/note/share.do",
        type: "post",
        data: {
            "noteId":noteId
        },
        dataType: "json",
        success:function(data) {
            let msg = data.msg;
            alert(msg);
            return false;
        },
        error : function(data) {
            alert("分享失败")
        }
    })

}
function searchNote(e){
    let code = e.keyCode;//得到键盘码
    if(code==13){//回车
        let noteTittle = $("#search_note").val();
        $.ajax({
            url: base_url +"/note/search.do",
            type: "get",
            data: {
                "noteTittle":noteTittle
            },
            dataType: "json",
            success:function(data) {
                if(data.status==0){
                    $(".col-xs-3").hide();
                    $("#pc_part_6").show();
                    let notes = data.data;
                    console.log(notes)
                    let msg=data.msg;
                    $("#search_list").empty();
                    for (var i = 0; i < notes.length; i++) {
                        var shareId=notes[i].shareId;
                        var noteName=notes[i].shareTittle;
                        var s_li ='<li class="online" >'
                        s_li += '<a>'
                        s_li += '<i class="fa fa-file-text-o" title="online" rel="tooltip-bottom">'
                        s_li += '</i> '+noteName
                        s_li += '</a>'
                        s_li += '</li>'
                        var $li = $(s_li)//将html字符串转换成jquery对象
                        $li.data("shareId",shareId)
                        //将li元素添加到ul
                        $("#search_list").append($li)
                    }
                    return false;
                }
            },
            error : function(data) {
                alert("查询失败")
            }
        })
    }
}
function showSearchDetails() {
    $("#search_list li a").removeClass("checked")
    $(this).find("a").addClass("checked")
    var shareId = $(this).data("shareId");
    $.ajax({
        url: base_url + "/note/loadShare.do",
        type: "get",
        data: {
            "shareId": shareId,
        },
        dataType: "json",
        success:function(data) {
            if (data.status==0) {
                var note=data.data;
                console.log(note)
                var noteTittle=note.shareTittle
                var noteBody=note.shareBody
                $("#input_note_title").val(noteTittle)
                var um = UM.getEditor('myEditor');
                um.setContent(noteBody)
                return false;
            }
        },
        error:function(XMLHttpRequest,textStatus,errorThrown) {
            alert("查询失败,稍后重试")
        }
    })
}
function moveNote(){
    var $noteLi=$("#note_list a.checked")
    if ($noteLi.length==0) {
        alert("请选择一个笔记")
    }
    else {
        //半透明背景显示
        $(".opacity_bg").show();
        //跳转页面
        var url=base_url + "/alert/alert_move.html"
        //can div中加载此页面
        $("#can").load(url,function () {
            var bookList=$("#book_list li")
            for (var i = 0; i < bookList.length; i++) {
                var bookId = $(bookList[i]).data("bookId");
                var bookName = $(bookList[i]).text();
                //动态拼接li标签内容,加到ul
                var s_li = '<option value='+bookId+'>'+bookName+'</option>'
                var $li = $(s_li)//将html字符串转换成jquery对象
                $li.data("bookId", bookId)//将bookId绑定到li元素中

                //将li元素添加到ul
                $("#moveSelect").append($li)
            }
        });

        return false;
    }
}